package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjCodePrintTemplateMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjCodePrintTemplateComponentService;
import com.zb.service.ZjCodePrintTemplateService;
import com.zb.vo.CodePrintTemplateListVo;
import com.zb.vo.CodePrintTemplateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 编码打印模板表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-07
 */
@Service
public class ZjCodePrintTemplateServiceImpl extends ServiceImpl<ZjCodePrintTemplateMapper, ZjCodePrintTemplate> implements ZjCodePrintTemplateService {
    @Resource
    private ZjCodePrintTemplateMapper codePrintTemplateMapper;
    @Resource
    private ZjCodePrintTemplateComponentService codePrintTemplateComponentService;

    // 保存或更新打印模板
    @Override
    @Transactional
    public void save(AccounLoginInfo loginInfo, CodePrintTemplateVo templateVo) {
        Assert.isTrue(null == codePrintTemplateMapper.existByName(loginInfo.getCustAccId(), templateVo.getId(), templateVo.getName()), "模板名称已重复！");

        ZjCodePrintTemplate template = new ZjCodePrintTemplate();
        BeanUtils.copyProperties(templateVo, template);
        template.setDisabled(templateVo.getStatus());
        template.setUpdateEmpId(loginInfo.getId());
        template.setUpdateEmp(loginInfo.getName());
        if (null != template.getId()) {
            this.updateById(template);
        } else {
            template.setId(IdWorker.getId());
            template.setCustAccId(loginInfo.getCustAccId());
            template.setCreateEmpId(loginInfo.getId());
            template.setCreateEmp(loginInfo.getName());
            template.setPrintDataNum(0);
            this.save(template);
        }
        codePrintTemplateComponentService.save(loginInfo.getCustAccId(), template.getId(), templateVo.getTemplateComponentVos(), templateVo.getDelTemplateComponentIds());
    }

    // 获取打印模板列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        QueryWrapper<ZjCodePrintTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        Page<ZjCodePrintTemplate> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<CodePrintTemplateListVo> voList = page.getRecords().stream().map(template -> {
            CodePrintTemplateListVo vo = new CodePrintTemplateListVo();
            BeanUtils.copyProperties(template, vo);
            vo.setStatus(template.getDisabled());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据模板ID获取模板组件详情
    @Override
    public CodePrintTemplateVo getDetailById(Long id) {
        ZjCodePrintTemplate template = this.getById(id);
        Assert.isTrue(null != template, "模板参数["+ id +"]的数据不存在！");
        CodePrintTemplateVo vo = new CodePrintTemplateVo();
        BeanUtils.copyProperties(template, vo);
        vo.setStatus(template.getDisabled());
        vo.setTemplateComponentVos(codePrintTemplateComponentService.getByPrintTemplateId(id));
        return vo;
    }

    // 根据模板ID批量启用或禁用打印模板
    @Override
    public void updateDisabledBatchById(AccounLoginInfo loginInfo, List<Long> idList, boolean status) {
        codePrintTemplateMapper.updateDisabledBatchById(loginInfo, idList, status);
    }

    // 根据模板ID批量删除打印模板
    @Override
    @Transactional
    public void deleteBatchById(List<Long> idList) {
        codePrintTemplateComponentService.deleteByPrintTemplateIds(idList);
        this.removeByIds(idList);
    }

    // 打印模板下拉列表查询
    @Override
    public Map<String, Object> codePrintTemplatePageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjCodePrintTemplate> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "height", "width", "code_num", "row_qty");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjCodePrintTemplate> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectCodeTemplateVo> voList = page.getRecords().stream().map(temp -> {
            return new SelectCodeTemplateVo(temp.getId(), temp.getName(), temp.getHeight(), temp.getWidth(), temp.getCodeNum(), temp.getRowQty());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
